package StudentManagementSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    private static final String ADD_STUDENT = "1";
    private static final String DELETE_STUDENT = "2";
    private static final String UPDATE_STUDENT = "3";
    private static final String QUERY_STUDENT = "4";
    private static final String EXIT = "5";
    public static void start() {
          ArrayList<Student> list = new ArrayList<>();
        loop:while (true) {
            Menu();
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose){
                case ADD_STUDENT-> addStudent(list);
                case DELETE_STUDENT-> deleteStudent(list);
                case UPDATE_STUDENT-> updateStudent(list);
                case QUERY_STUDENT-> queryStudent(list);
                case EXIT-> {
                    System.out.println("退出系统,欢迎您下次使用");
                    break loop;
                }
                default -> System.out.println("没有这个选项,请重新输入:");
        }
        }
    }
    //菜单
    public static void Menu(){
        System.out.println("*****************欢迎来到学生管理系统*****************");
        System.out.println("1.添加学生");
        System.out.println("2.删除学生");
        System.out.println("3.修改学生");
        System.out.println("4.查询学生");
        System.out.println("5.退出系统");
        System.out.print("请输入你需要的数字:");
    }
    //添加学生信息
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        Student stu = new Student();
        System.out.println("请输入您要添加学生的id:");
        while (true) {
            String id = sc.next();
            if (contains(list, id)) {
                System.out.println("当前ID学生存在，请重新输入：");
            }else {
                System.out.println("请输入您要添加学生的姓名:");
                String name = sc.next();
                System.out.println("请输入您要添加学生的年龄:");
                int age = sc.nextInt();
                System.out.println("请输入您要添加学生的地址:");
                String address = sc.next();
                stu.setId(id);
                stu.setName(name);
                stu.setAge(age);
                stu.setAddress(address);
                list.add(stu);
                System.out.println("学生信息 添加成功！");
                break;
            }
        }
    }
    //删除学生
    public static void deleteStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除学生的ID：");
        while (true) {
            String id = sc.next();
            int result = index(list,id);
            if (result>=0) {
                list.remove(result);
                System.out.println("此学生删除成功！");
                break;
            }
             else {
                System.out.println("无此id的学生，请重新输入:");
            }
        }
    }
    //修改学生
    public static void updateStudent(ArrayList<Student> list){
        System.out.println("请输入您要修改学生的id:");
        jk:while (true){
            Scanner sc = new Scanner(System.in);
            String id =sc.next();
            int result = index(list,id);
            Student stu = new Student();
            if (result>=0){
                stu = list.get(result);
                System.out.println("当前ID存在");
                System.out.println("请输入要修改的学生的名称:");
                String rename = sc.next();
                stu.setName(rename);
                System.out.println("请输入要修改学生的年龄:");
                int age = sc.nextInt();
                stu.setAge(age);
                System.out.println("请输入要修改学生的家庭地址:");
                String newaddress = sc.next();
                stu.setAddress(newaddress);
                System.out.println("学生信息修改成功");
                break jk;
            }if(result==-1){
                System.out.println("不存在该ID请重新输入:");

            }

        }

    }
    //查询所有学生信息
    public static void queryStudent(ArrayList<Student> list){
        if(list.size()==0){
            System.out.println("当前无学生信息，请添加后再查询");
            return;
        }else {
        System.out.println("id\t姓名\t年龄\t家庭地址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }
        }


    }

    public static boolean contains(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (id.equals(stu.getId())){
                return true;
            }
        }
        return false;
    }
    public static int index(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String sid = stu.getId();
            if (sid.equals(id)){
                return i;
            }
        }
        return -1;
    }
}
